exports.AnalyticsHandler = Core.Class.singleton( {
	classname: 'AnalyticsHandler',
	
	initialize: function()
	{
		this.enabled = true;
        if (this.enabled == true )
        {
            Core.Analytics.enable();
        }
	},

	userJson: function()
	{
		if(this.enabled)
		{
			var _user = MMGameState.getCurrentUser();
			var _userJson = {"coins":_user.getCoins()+'',"bucks":_user.getNuggets()};
			return _userJson;
		}
	},
	gameConnect: function()
	{
		if(this.enabled)
		{
			Core.Analytics.reportGameEvent("CONNECT",{"username":MMGameState.getCurrentUser().getGamerTag()},this.userJson());
		}
	},
	transactionJson: function(id)
	{
		if(this.enabled)
		{
			var JSONObject = GAGameLoader.getStoreHandler().getStoreObjectForKey(id);
			var itemID = JSONObject.visible_id;
			var pvid = JSONObject.parent.visible_id;
			var pname = JSONObject.parent.name;
			var ppvid = JSONObject.parent.parent.visible_id;
			var ppname = JSONObject.parent.parent.name;
			
			var _user = MMGameState.getCurrentUser();
			
			var _userJson = {"ItemID":itemID+'',"CatID":pvid+'',"CatName":pname+'',"StoreID":ppvid+'',"StoreName":ppname,"Quantity":1+'',"coins":_user.getCoins()+'',"bucks":_user.getNuggets()};
			return _userJson;
		}
	},
	
	stateChange: function(_name,_currency,_amount)
	{
		if(this.enabled)
		{
			Core.Analytics.reportPlayerStateChange(_name,{_currency:_amount},this.userJson());
		}
	},
	
	funnelStage: function(_name,id,_destination)
	{
		if(this.enabled)
		{
			Core.Analytics.reportFunnelStage(_name,id,_destination);
		}
	},
	
	gameEvent: function(_eventName)
	{
		if(this.enabled)
		{
			Core.Analytics.reportGameEvent(_eventName,{"username":MMGameState.getCurrentUser().getGamerTag()},this.userJson());
		}
	},
	visitEvent:function(_neighbourName, _random)
	{
		if(this.enabled)
		{
			if (_random){
				Core.Analytics.reportGameEvent("Neighbour",{"VisitNeighborRandom":_neighbourName},this.userJson());
			}
			else{
				Core.Analytics.reportGameEvent("Neighbour",{"VisitNeighborFriend":_neighbourName},this.userJson());
			}
		}
	},
	
	logTransaction : function (object) 
	{
		if(this.enabled)
		{
			this.logAnalyticsTransaction(object);	
		}
	},
	
	logSell: function(_object)
	{
		if(this.enabled)
		{
			this.logInstant(_object,"TRANSACTIONSELL");
		}
	},
	
	logInstant: function(id,name)
	{
		if(this.enabled)
		{	
			var JSONObject = GAGameLoader.getStoreHandler().getStoreObjectForKey(id);
			var itemID = JSONObject.visible_id;
			var pvid = JSONObject.parent.visible_id;
			var pname = JSONObject.parent.name;
			var ppvid = JSONObject.parent.parent.visible_id;
			var ppname = JSONObject.parent.parent.name;
			var sellPrices = GACommonUtils.getSellPrice(JSONObject);
			var type = TCUtil.getCurrency(sellPrices);
			
			var cost = TCUtil.getPrice(sellPrices);
			
			var _user = MMGameState.getCurrentUser();
			
			var _userJson = {"ItemID":itemID+'',"CatID":pvid+'',"CatName":pname+'',"StoreID":ppvid+'',"StoreName":ppname,"Currency":type,"Price":cost+'',"Quantity":1+'',"Level":_user.getLevel()+'',"coins":_user.getCoins()+'',"bucks":_user.getRc()+'',"xp":_user.getXp()+''/*,"gridsize":_user.getGridSize()+'' */};
			if (type == "coins")
			{
				Core.Analytics.reportPlayerStateChange(name,{"StoreName":pname,"coins":cost},_userJson);	
			}
			else
			{
				Core.Analytics.reportPlayerStateChange(name,{"StoreName":pname,"bucks":cost},_userJson);
			}
		}
	},
	
	logAnalyticsTransaction: function (id) 
	{
		if(this.enabled)
		{
			var JSONObject = GAGameLoader.getStoreHandler().getStoreObjectForKey(id);
//			var itemID = JSONObject.visible_id;
			var itemName = JSONObject.name;

//			var pvid = JSONObject.parent.visible_id;
//			var pname = JSONObject.parent.name;
//			var ppvid = JSONObject.parent.parent.visible_id;
//			var ppname = JSONObject.parent.parent.name;
//			var sellPrices = GACommonUtils.getSellPrice(JSONObject);
			var priceForBucket = JSONObject.buy_prices["default"];
			var type = TCUtil.getCurrency(priceForBucket);
			
			var cost = TCUtil.getPrice(priceForBucket);
			
			var _user = MMGameState.getCurrentUser();
			
			var TEST = this.transactionJson(id);
//			
//			console.log("zaki::::: COST "+cost);
//			console.log("zaki::::: transactionJson"+JSON.stringify(TEST));
			
			if (type == "coins")
			{
				Core.Analytics.reportPlayerStateChange("Transactionbuy",{"ItemName":itemName,"coins":cost},this.transactionJson(id));	
			}
			else
			{
				Core.Analytics.reportPlayerStateChange("Transactionbuy",{"ItemName":itemName,"nuggets":cost},this.transactionJson(id));
			}
		}
	},
	
	logQuestUnlock: function(name)
	{
		if(this.enabled)
		{
			this.funnelStage(name+"Unlocked",1,name+"Started");
		}
	},
	
	logInstantComplete: function(_job,_cost)
	{
		if(this.enabled)
		{
			Core.Analytics.reportPlayerStateChange("Transaction",{"InstantJob":_job,"cost":_cost});
		}
	},
	
	logSpeedUp: function(_job,_cost)
	{
		if(this.enabled)
		{
			Core.Analytics.reportPlayerStateChange("Transaction",{"StoreName":"FactoryJobSpeedUp","coins":_cost});
		}
	},
	
	logQuestComplete: function(name)
	{
		if(this.enabled)
		{
			this.funnelStage(name+"Started",1,name+"Completed");
		}
	},
	
	logEvent : function (eventType) 
	{
		if(this.enabled)
		{
			this.logAnalyticsEvent(eventType);
		}
	},
	
	reportNavigation: function(from,to,button)
	{
		if(this.enabled)
		{
			Core.Analytics.reportNavigation(from, to, button);
		}
	},
	
	logAnalyticsEvent : function (eventType) 
	{
		if(this.enabled)
		{
			var _user = MMGameState.getCurrentUser();
			Core.Analytics.reportGameEvent(eventType,{"username":_user.getGamerTag()},{"Coins":_user.getCoins(),"Bucks":_user.getNuggets()});	
		}
	},
	
	logOffer : function (offerType, currency, currencyName, offerId) 
	{
		if(this.enabled)
		{
			this.logAnalyticsOffer(offerType, currency, currencyName, offerId);
		}
	},
	
	logAnalyticsOffer: function (offerType, currency, currencyName, offerId) 
	{
		if(this.enabled)
		{
			var _user = MMGameState.getCurrentUser();
			Core.Analytics.reportPlayerStateChange(offerType,{"CurrencyName":currencyName+'',"Currency":currency+''},{"Currency":currency+'',"OfferId":offerId+''});
		}
	}
});